package com.credithc.eagleeye.api.init;
import com.credithc.eagleeye.api.config.ApplicationConfig;
import com.credithc.eagleeye.api.job.AlertJobManager;
import com.credithc.eagleeye.api.service.SettingService;
import com.credithc.eagleeye.api.utils.JWTUtils;
import com.credithc.eagleeye.api.utils.SpringContextUtils;
import com.credithc.eagleeye.api.vo.AlertRuleVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.util.List;
/**
 * 描 述: <br/>
 * 作 者: zhangjin<br/>
 * 创 建：2018年02月06日<br/>
 * 版 本：v1.0.0<br>
 * <p>
 * 历 史: (版本) 作者 时间 注释 <br/>
 */
@Slf4j
@Component
public class InitRunner implements ApplicationRunner {
    @Autowired
    private ApplicationContext applicationContext;
    @Autowired
    private ApplicationConfig applicationConfig;
    @Autowired
    private AlertJobManager alertJobManager;
    @Autowired
    private SettingService settingService;
    @Override
    public void run(ApplicationArguments args) throws Exception {
        log.info("eagle-eye-api-boot init run starting...");
        SpringContextUtils.init(applicationContext);
        JWTUtils.getInstance().init(applicationConfig.getJwtSecretKey(),applicationConfig.getJwtExpireTimeSeconds(),applicationConfig.getSecretKeySeed(),applicationConfig.getIvParameterSeed());
        List<AlertRuleVo> alertRuleVoList = settingService.loadAllAlertRule();
        for(AlertRuleVo ruleVo : alertRuleVoList) {
            alertJobManager.addOrUpdate(ruleVo);
        }
        log.info("eagle-eye-api-boot init run success...");
    }
}
